package com.xuchao.rabbit.b3;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.xuchao.rabbit.utils.RabbitMqUtils;
import com.xuchao.rabbit.utils.SleepUtils;

/**
 * @Author xuchao
 * @Date:2023/6/6 19:08
 * @description TODO
 * @Version 1.0
 */
public class Worker03 {
    //队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";
    //接收消息
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("c3等待接受消息，处理时间较长");
        DeliverCallback deliverCallback = (consumerTage, message)->{
            //沉睡10秒
            SleepUtils.sleep(5);
            System.out.println(new String(message.getBody(),"UTF-8"));
            //手动应答
            //1.消息的标记 2.是否批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        CancelCallback cancelCallback = consumerTage -> {
            System.out.println("消息消费被中断:"+consumerTage);
        };
        //设置不公平分发
        int prefetchCount = 5;
        channel.basicQos(prefetchCount);
        //1.队列名 2.是否自动应答 3.消费者未成功消费的回调 4.消费者消费被中断的回调
        channel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback,cancelCallback);
    }
}
